<template>
  <div 
    class="square-news-card"
    :class="{
      'hot-news': news.isHot,
      'new-news': news.isNew
    }"
    @click="openNews"
  >
    <div class="card-image" :style="imageStyle">
      <div class="category-tag">
        <el-tag size="small" :type="getTagType(news.category)">
          {{ news.category }}
        </el-tag>
      </div>
      <div v-if="news.isHot" class="hot-badge">🔥 热点</div>
      <div v-if="news.isNew" class="new-badge">🆕 最新</div>
    </div>
    
    <div class="card-content">
      <h3 class="title">{{ news.title }}</h3>
      
      <div class="card-meta">
        <div class="source">{{ news.source }}</div>
        <div class="time">{{ news.time }}</div>
      </div>
      
      <div class="card-actions">
        <el-button 
          :icon="isFavorite ? 'StarFilled' : 'Star'" 
          circle 
          size="small"
          @click.stop="handleToggleFavorite"
          :type="isFavorite ? 'primary' : ''"
        />
        <el-tag v-if="news.hotValue > 300000" type="danger" size="small">
          {{ formatHotValue(news.hotValue) }}
        </el-tag>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { useNewsStore } from '@/stores/news';

const props = defineProps({
  news: {
    type: Object,
    required: true
  }
});

const newsStore = useNewsStore();
const isFavorite = computed(() => newsStore.isFavorite(props.news.id));

// 获取标签类型
const getTagType = (category) => {
  const types = ['', 'success', 'info', 'warning', 'danger'];
  const index = newsStore.categories.findIndex(c => c.id === category) % 5;
  return types[index];
};

// 格式化热度值
const formatHotValue = (value) => {
  if (value >= 1000000) return (value / 1000000).toFixed(1) + 'm';
  if (value >= 1000) return (value / 1000).toFixed(1) + 'k';
  return value;
};

// 图片样式
const imageStyle = computed(() => ({
  backgroundImage: `url(${props.news.image})`,
  backgroundSize: 'cover',
  backgroundPosition: 'center'
}));

// 打开新闻
const openNews = () => {
  window.open(props.news.url, '_blank');
};

// 处理收藏切换
const handleToggleFavorite = () => {
  newsStore.toggleFavorite(props.news.id);
};
</script>

<style scoped>
.square-news-card {
  width: 100%;
  height: 100%;
  border-radius: 12px;
  overflow: hidden;
  background: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  position: relative;
}

.square-news-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}

.square-news-card.hot-news {
  border-top: 3px solid #f56c6c;
}

.square-news-card.new-news {
  border-top: 3px solid #409EFF;
}

.card-image {
  height: 200px;
  position: relative;
}

.category-tag {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 10;
}

.hot-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(245, 108, 108, 0.9);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  z-index: 10;
}

.new-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(64, 158, 255, 0.9);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  z-index: 10;
}

.card-content {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.title {
  margin: 0 0 12px 0;
  font-size: 16px;
  line-height: 1.4;
  font-weight: 600;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 12px;
  color: #666;
}

.source {
  font-weight: 500;
  color: #409EFF;
}

.time {
  color: #999;
}

.card-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}
</style>